package com.rpframework.module.common.bottom.entity;


import com.rpframework.module.common.bottom.entity.enums.RequestType;
import com.rpframework.utils.ArrayUtils;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * request URL描述
 */
public class Jurisdiction extends Base {
    /**
     * 描述
     */
    private String introduce;
    /**
     * 上层描述信息
     */
    private Long fixId;
    /**
     * 是否取消认证
     */
    private Boolean cancel;
    /**
     * 接口类型
     */
    private RequestType requestType;

    public Jurisdiction(String introduce, String[] uri, Boolean cancel, RequestType requestType) {
        this.introduce = introduce;
        this.uri = uri;
        this.cancel = cancel;
        this.requestType = requestType;
    }

    /**
     * URI
     */
    @Transient
    private String[] uri;
    @Transient
    private List<JurisdictionUri> jurisdictionUris;
    /**
     * 子链接
     */
    @Transient
    private List<Jurisdiction> urlDescribes;

    private List<Jurisdiction> urlDescribes() {
        if (urlDescribes == null) urlDescribes = new ArrayList<>();
        return urlDescribes;
    }

    /**
     * URL转实体类
     *
     * @param uris           地址
     * @param jurisdictionId 权限主键
     * @return 实体
     */
    private static List<JurisdictionUri> uriParseEntity(String[] uris, Long jurisdictionId) {
        if (ArrayUtils.isEmpty(uris)) {
            return null;
        }
        List<JurisdictionUri> jurisdictionUri = new ArrayList<>();
        for (String uri :
                uris) {
            jurisdictionUri.add(new JurisdictionUri(uri, jurisdictionId));
        }
        return jurisdictionUri;
    }

    /**
     * 将数据库查出数据转换成View数据
     *
     * @param jurisdictions db
     * @return View
     */
    public static List<Jurisdiction> dBParseView(List<Jurisdiction> jurisdictions) {
        List<Jurisdiction> parse = new ArrayList<>();
        List<Jurisdiction> fix = new ArrayList<>();
        List<Jurisdiction> res = new ArrayList<>();
        //拆分关系
        for (Jurisdiction ju : jurisdictions) {
            if (ju.getFixId() != null) fix.add(ju);
            else parse.add(ju);
        }
        for (Jurisdiction p : parse) {
            fix.stream().filter(c -> Objects.equals(c.getFixId(), p.getId())).forEach(c -> p.urlDescribes().add(c));
            res.add(p);
        }
        return res;
    }

    /**
     * URL转实体类
     *
     * @param uris 地址
     * @return 实体
     */
    public List<JurisdictionUri> uriParseEntity(String[] uris) {
        return uriParseEntity(uris, getId());
    }

    public Jurisdiction() {
    }

    public RequestType getRequestType() {
        return requestType;
    }

    public void setRequestType(RequestType requestType) {
        this.requestType = requestType;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }

    public String[] getUri() {
        return uri;
    }

    public void setUri(String[] uri) {
        this.uri = uri;
    }

    public Boolean getCancel() {
        return cancel;
    }

    public void setCancel(Boolean cancel) {
        this.cancel = cancel;
    }

    public List<Jurisdiction> getUrlDescribes() {
        return urlDescribes;
    }

    public void setUrlDescribes(List<Jurisdiction> urlDescribes) {
        this.urlDescribes = urlDescribes;
    }

    public List<JurisdictionUri> getJurisdictionUris() {
        return jurisdictionUris;
    }

    public void setJurisdictionUris(List<JurisdictionUri> jurisdictionUris) {
        this.jurisdictionUris = jurisdictionUris;
    }

    public Long getFixId() {
        return fixId;
    }

    public void setFixId(Long fixId) {
        this.fixId = fixId;
    }
}
